<?php


namespace app\common\model;

use app\common\model\Banner;
use app\common\model\CateGoods;
use app\common\model\Goods;
use think\Db;
use think\Model;
class Index extends Model
{

    // 首页信息
    public function indexInfo()
    {
        // 获取 url
        $url    = config('fastadmin.api_url');
        // 轮播图
        $banner = banner::where('status',1)->field('id,image')->select();
        foreach($banner as $key => $val)
        {
            $banner[$key]['image'] = $url.$val['image'];
        }
        // 商品分类
        $cate_goods = CateGoods::where(['status'=>1,'pid'=>0])->field('id,title,image')->select();
        foreach($cate_goods as $key => $val)
        {
            $cate_goods[$key]['image'] = $url.$val['image'];
        }
        // 限时秒杀
        $seckill_goods  = Db::name('goods')
            ->alias('g')
            ->join('goods_seckill gs','g.id = gs.good_id')
            ->field('g.id,g.name,g.image,g.money')
            ->select();
        if($seckill_goods)
        {
            foreach($seckill_goods as $key => $val)
            {
                $seckill_goods[$key]['image'] = $url.$val['image'];
            }
        }
        // 是否推荐
        $recom = [['id'=>0,'title'=>'推荐']];
        $goods = Db::name('cate_goods')->where('pid','>',0)->where('status',1)->field('id,title')->select();
        // 合并数组
        $merge = array_merge($recom,$goods);
        foreach($merge as $key => &$val)
        {
            if(empty($val['id']))
            {
                $val['goods'] = Db::name('goods')->where('state',2)->field('id,image,name,integer,money')->select();
                foreach($val['goods'] as &$val1)
                {
                    $val1['image'] = $url.$val1['image'];
                }
            }else{
                $val['goods'] = Db::name('goods')->where('cate_id',$val['id'])->field('id,image,name,integer,money')->select();
                foreach($val['goods'] as &$val1)
                {
                    $val1['image'] = $url.$val1['image'];
                }
            }
        }

        $result = ['banner'=>$banner,'cate_goods'=>$cate_goods,'seckill_goods'=>$seckill_goods,'goods'=>$merge];
        return $result;
    }

}